package com.itboye.sunsun.login.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.itboye.sunsunhome.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.controller.LoginController;
import com.itboye.sunsun.controller.LoginedState;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

public class BindWeChatActivity extends BaseActivity implements OnClickListener{
	
	private EditText phoneedit;
	private EditText yzmedit;
	private EditText passwordedit;
	private Button yzmbutton;
	private String username;
	private Button loginbutton;
	private TimeCount time;
	private RelativeLayout backrela;
	protected String code;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_bindwechat);
		time = new TimeCount(60000, 1000,yzmbutton);// 构造CountDownTimer对象
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.yzmbutton:
			username=phoneedit.getText().toString();
			if(!username.equals(""))
			{
			   getyzm();
			   time.start();
			}else
			{
				Toast.makeText(BindWeChatActivity.this, "请输入手机号", 0).show();
			}
			break;
		case R.id.loginbutton:
			String uid=getIntent().getStringExtra("uid");
			String code=yzmedit.getText().toString();
			if(code==null){
				Toast.makeText(this, "请输入验证码", 0).show();
				return ;
			}
			code=code.trim();
			bind(code,uid);
			break;
		case R.id.backrela:
			this.finish();
			break;

		default:
			break;
		}
		
	}
	public void getyzm()
	{
		MyJsonRequest<String>request= new MyJsonRequest.Builder<String>()
				 .apiVer("100")
				 .typeKey("BY_Message_send_sms")
				 .param("mobile", phoneedit.getText().toString())
				 .param("code_type", "3")
				 .requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());	
						code=response.toString();
						yzmedit.setText(""+response);
					
					
					}
				})
				 .errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if(exception instanceof CodeErrorException)
						{
							Toast.makeText(App.ctx, msg, 0).show();
						}else
						{
							Toast.makeText(App.ctx, exception.toString(), 0).show();
						}
						
					}
				})
				.build();
				HttpRequest.getDefaultRequestQueue().add(request);
	}
	public void bind(String code,String uid)
	{
		MyJsonRequest<String>request= new MyJsonRequest.Builder<String>()
				 .apiVer("100")
				 .typeKey("BY_User_bind")
				 .param("mobile", phoneedit.getText().toString())
				 .param("password", passwordedit.getText().toString())
				 .param("code", code)
				 .param("uid",uid)
				 .requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());	
						//Toast.makeText(App.ctx, response.toString(), 0).show();
						SPUtils.put(App.ctx, null, SPContants.MOBILE, phoneedit.getText().toString());
						//发送广播通知LoginActivity结束
						Intent intent1=new Intent(LoginActivity.LOGINEXIT);
						sendBroadcast(intent1);
					//	LoginController.setLoginState(new LoginedState());
						BindWeChatActivity.this.finish();
						
						
						SPUtils.put(App.ctx, null, SPContants.PASSWORD, response);
                         //设置登录状态
						LoginController.setLoginState(new LoginedState());
						
						
						SPUtils.put(App.ctx, null, SPContants.MOBILE, phoneedit.getText().toString());
						Intent intent2 = new Intent("changeavart");
						sendBroadcast(intent2);
					
					}
				})
				 .errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if(exception instanceof CodeErrorException)
						{
							Toast.makeText(App.ctx, msg, 0).show();
						}else
						{
							Toast.makeText(App.ctx, exception.toString(), 0).show();
						}
						
					}
				})
				.build();
				HttpRequest.getDefaultRequestQueue().add(request);
	}
	
	

}
